package com.cy.pj.sys.service.impl;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.pojo.PageObject;
import com.cy.pj.sys.dao.SysLogDao;
import com.cy.pj.sys.pojo.SysLog;
import com.cy.pj.sys.service.SysLogService;
import com.sun.glass.ui.Size;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 张琪
 * @date 2020/11/13
 * @describe
 */

@Service
public class SysLogServiceImpl implements SysLogService {

    @Autowired
    private SysLogDao sysLogDao;

    // get/set方法实现功能

    @Override
    public PageObject findPageObjects(String username, Integer pageCurrent) {
//        1. 参数检验
        if(pageCurrent == null || pageCurrent < 1)
            throw new IllegalArgumentException("当前页码不正确");
//        2. 查询总数
        Integer rows = sysLogDao.getRowCount(username);
        if(rows==0)
            throw new ServiceException("系统没有查到对应记录");
//        3. 查询当前页记录
        int pageSize = 15;
        int startIndex = (pageCurrent-1) * 15;
        List<SysLog> records = sysLogDao.findPageObjects(username, startIndex, pageSize);

        int PageCount = rows/ 15;
        if(rows % 15 != 0 || PageCount == 0)
            PageCount++;

        return new PageObject(rows, records, PageCount, pageSize, pageCurrent);
    }

    @Override
    public int doDeleteObjects(Integer[] logIds) {
        int rows = sysLogDao.doDeleteObjects(logIds);
        return rows;
    }

    @Override
    public int saveObject(SysLog sysLog) {
        int rows = sysLogDao.insertObject(sysLog);
        return rows;
    }
}
